package gui.cliente;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JFrame;

/**
 * @author tiagosn
 */
public class JanelaConectar extends JFrame {
    public JanelaConectar() {
        initComponents();
        jlbIPInvalido.setVisible(false);
        jlbPortaInvalida.setVisible(false);
        jtfLogin.requestFocus();

        jbtConectar.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String ip = jtfIPServidor.getText();
                    String porta = jtfPorta.getText();
                    boolean ipValido = ipValido(ip);
                    boolean portaValida = portaValida(porta);

                    if(ipValido && portaValida){
                        jlbIPInvalido.setVisible(false);
                        jlbPortaInvalida.setVisible(false);
                        /// terminar
                    }else{
                        jlbIPInvalido.setVisible(!ipValido);
                        jlbPortaInvalida.setVisible(!portaValida);
                    }
                }
            }
        );
    }

    private boolean ipValido(String ip){
        StringTokenizer st = new StringTokenizer(ip, ".");
        int parteIP;
        int cnt;

        for(cnt = 0; st.hasMoreTokens(); ++cnt){
            try{
                parteIP = Integer.parseInt(st.nextToken());
            }catch(NumberFormatException e){
                return false;
            }
            if(parteIP < 0 || parteIP > 255){
                return false;
            }
        }
        return(cnt == 4);
    }

    private boolean portaValida(String porta){
        try{
            int numeroPorta = Integer.parseInt(porta);

            return(numeroPorta >= 0 && numeroPorta <= 65535);
        }catch(NumberFormatException e){
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlbLogin = new javax.swing.JLabel();
        jtfLogin = new javax.swing.JTextField();
        jtfIPServidor = new javax.swing.JTextField();
        jlbIPServidor = new javax.swing.JLabel();
        jbtConectar = new javax.swing.JButton();
        jlbPorta = new javax.swing.JLabel();
        jtfPorta = new javax.swing.JTextField();
        jlbPortaInvalida = new javax.swing.JLabel();
        jlbIPInvalido = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login - JLocalChat");
        setResizable(false);

        jlbLogin.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jlbLogin.setText("Login:");

        jlbIPServidor.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jlbIPServidor.setText("IP do servidor:");

        jbtConectar.setText("Conectar");

        jlbPorta.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jlbPorta.setText("Porta:");

        jlbPortaInvalida.setForeground(new java.awt.Color(192, 18, 18));
        jlbPortaInvalida.setText("porta inválida!!!");

        jlbIPInvalido.setForeground(new java.awt.Color(192, 18, 18));
        jlbIPInvalido.setText("IP inválido!!!");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlbIPServidor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlbIPInvalido)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbtConectar)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jtfIPServidor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                            .addComponent(jtfLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlbPorta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlbPortaInvalida)
                        .addContainerGap(119, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtfPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlbLogin)
                        .addContainerGap(224, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlbLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbIPServidor)
                    .addComponent(jlbIPInvalido))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfIPServidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbPorta)
                    .addComponent(jlbPortaInvalida))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jbtConectar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtConectar;
    private javax.swing.JLabel jlbIPInvalido;
    private javax.swing.JLabel jlbIPServidor;
    private javax.swing.JLabel jlbLogin;
    private javax.swing.JLabel jlbPorta;
    private javax.swing.JLabel jlbPortaInvalida;
    private javax.swing.JTextField jtfIPServidor;
    private javax.swing.JTextField jtfLogin;
    private javax.swing.JTextField jtfPorta;
    // End of variables declaration//GEN-END:variables
}
